﻿using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using mediaPrime.Sdk;

namespace mediaPrime.Plugin.SystemManagement.UserControls
{
    /// <summary>
    /// Interaktionslogik für SettingsUserControl.xaml
    /// </summary>
    public partial class SettingsUserControl : UserControl
    {
        public SettingsUserControl(string id, string group)
        {
            InitializeComponent();

            listBoxSettings.ItemsSource = Settings.Apps[id].UserSettings.Where(us => us.GroupName == group);
        }

        private void listBoxSettings_ItemClicked(object sender, Controls.MPMenuItemClickedEventArgs e)
        {
            var setting = e.Item as Setting;
            if (setting != null)
            {
                var type = setting.GetSettingsType();

                if (type == typeof(SettingEnumValue))
                {
                    string currentValue = null;
                    var settingEnumValue = setting.GetValue<SettingEnumValue>();
                    if (settingEnumValue != null && !string.IsNullOrEmpty(settingEnumValue.Text))
                        currentValue = settingEnumValue.Text;

                    var dlg = new Dialogs.SelectEnumValueDialog(setting.Values.Select(s => s.Text).ToList(), currentValue);
                    if (dlg.ShowDialog() == true)
                    {
                        var newValue = setting.Values.Where(v => v.Text == dlg.Value).FirstOrDefault();
                        if (newValue != null && newValue != settingEnumValue)
                            setting.SetValue(newValue);
                    }
                }
                else if (type == typeof(bool))
                {
                    var currentValue = setting.GetValue<bool>(false);
                    setting.SetValue(!currentValue);
                }
                else if (type == typeof(int))
                {
                    var currentValue = setting.GetValue<int>();

                    var dlg = new Dialogs.IntegerInputDialog(currentValue.ToString());
                    if (dlg.ShowDialog() == true)
                    {

                    }
                }
                else
                {
                    var settingsUserControl = SystemPlugin.Instance.SendMessageWithResult(setting.AppID, QueryTypes.CustomSettingsDialog, setting) as CustomSettingsDialog;
                    if (settingsUserControl != null)
                    {
                        settingsUserControl.Load(setting);

                        var dlg = new Dialogs.CustomSettingsDialog(settingsUserControl);
                        dlg.Title = setting.DisplayName;
                        if (dlg.ShowDialog() == true)
                        {
                            settingsUserControl.Save(setting);
                        }
                    }
                }

            }
        }
    }

    public class SettingsDataTemplateSelector : DataTemplateSelector
    {

        public DataTemplate BoolTemplate { get; set; }
        public DataTemplate DefaultTemplate { get; set; }
        public DataTemplate EmptyTemplate { get; set; }
        public DataTemplate EnumTemplate { get; set; }

        public override DataTemplate SelectTemplate(object item, DependencyObject container)
        {
            if (item is Setting)
            {
                var setting = item as Setting;

                var value = setting.GetValue();

                Type type = null;
                if (value != null)
                    type = value.GetType();

                if (type == typeof(bool))
                    return BoolTemplate;

                else if (type == typeof(SettingEnumValue))
                    return EnumTemplate;
                else
                {
                    return DefaultTemplate;
                }
            }
            else
            {
                return EmptyTemplate;
            }

        }
    }

    public class DisplayNameConverter : IValueConverter
    {

        #region IValueConverter Member

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            var setting = value as Setting;
            if (setting is Setting)
            {
                if (setting.IsDefaultSettingsType)
                    return value;

                var displayName = SystemPlugin.Instance.SendMessageWithResult(setting.AppID, QueryTypes.CustomSettingDisplayName, setting) as string;
                if (string.IsNullOrEmpty(displayName))
                    return setting.Value;
                else
                    return displayName;
            }
            else
                return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
